﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.Collections;


public class Class1 : MonoBehaviour
{
    void Start()
    {
        
        //UploadSnapshot("002",  delegate(bool re) { print(re); });
    }
    public void UploadSnapshot(string fileName, Action<bool> result)
    {
        //Debug.Log("url:" + url);

        StartCoroutine(_UploadSnapshot(fileName, result));
    }
    private IEnumerator _UploadSnapshot(string fileName, Action<bool> result)
    {
        Camera g = GameObject.Find("Camera").camera;
        g.enabled = true;
        g.Render();
        RenderTexture.active = g.targetTexture;
        g.pixelRect = new Rect(0, 0, 64, 64);
        Texture2D tex = new Texture2D((int)g.pixelRect.width, (int)g.pixelRect.height, TextureFormat.RGB24, false);
        tex.ReadPixels(g.pixelRect, 0, 0);
        tex.Apply();
        g.enabled = false;

        byte[] bytes = null;
        JPGEncoder encoder = new JPGEncoder(tex, 90.0f);
        while (!encoder.isDone)
            yield return new WaitForSeconds(.3f);
        bytes = encoder.GetBytes();
        Texture.Destroy(tex);

        WWWForm form = new WWWForm();
        form.headers["enctype"] = "multipart/form-data";
        form.AddBinaryData("PngUpload", bytes, fileName, "image/jpeg");
        WWW post = new WWW("http://127.0.0.1/upload_snapshot.php", form);
        //WWW post = new WWW("http://graph.facebook.com/me/photos?access_token", form);
        while (!post.isDone)
        {
            yield return new WaitForSeconds(.3f);
        }
        print(post.error);
        if (!string.IsNullOrEmpty(post.text) && string.Compare(post.text, "ok") == 0)
        {
            result(true);
        }
        else
        {
            result(false);
        }
        post.Dispose();
    }
    //public void UploadSnapshot(string fileName, string text, Rect rect, Action<bool> result)
    //{
    //    //Debug.Log("url:" + url);

    //    StartCoroutine(_UploadSnapshot(fileName, text, rect, result));
    //}
    //private IEnumerator _UploadSnapshot(string fileName, string text, Rect rect, Action<bool> result)
    //{
    //    yield return new WaitForEndOfFrame();

    //    Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
    //    tex.ReadPixels(new Rect(rect.left, Screen.height - rect.height - rect.top, rect.width, rect.height), 0, 0);
    //    tex.Apply();

    //    byte[] bytes = null;
    //    JPGEncoder encoder = new JPGEncoder(tex, 90.0f);
    //    while (!encoder.isDone)
    //        yield return new WaitForSeconds(.3f);
    //    bytes = encoder.GetBytes();
    //    Texture.Destroy(tex);

    //    WWWForm form = new WWWForm();
    //    form.headers["enctype"] = "multipart/form-data";
    //    form.AddField("text", text);
    //    form.AddBinaryData("PngUpload", bytes, fileName, "image/jpeg");
    //    WWW post = new WWW("http://127.0.0.1/upload_snapshot.php", form);
    //    //WWW post = new WWW("http://graph.facebook.com/me/photos?access_token", form);
    //    while (!post.isDone)
    //    {
    //        yield return new WaitForSeconds(.3f);
    //    }
    //    if (!string.IsNullOrEmpty(post.text) && string.Compare(post.text, "ok") == 0)
    //    {
    //        result(true);
    //    }
    //    else
    //    {
    //        result(false);
    //    }
    //    post.Dispose();
    //}
    void OnTriggerEnter(Collider other)
    {
        print("ok");
    }
}

